package cn.tedu.egbank.transaction.controller;

import cn.tedu.egbank.base.response.JsonResult;
import cn.tedu.egbank.transaction.mapper.TransactionMapper;
import cn.tedu.egbank.transaction.pojo.dto.DepositParam;
import cn.tedu.egbank.transaction.pojo.dto.TransferParam;
import cn.tedu.egbank.transaction.pojo.dto.WithdrawParam;
import cn.tedu.egbank.transaction.pojo.entity.Transaction;
import cn.tedu.egbank.transaction.pojo.vo.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@RestController
@Tag(name = "03-交易模块", description = "30")
@RequestMapping("/api/transactions/")
public class TransactionController {
    @Autowired
    private TransactionMapper transactionMapper;

    @PostMapping("deposit")
    @Operation(summary = "存款")
    @ApiOperationSupport(order = 10)
    public JsonResult deposit(@RequestBody DepositParam depositParam) {
        log.debug("depositParam="+ depositParam);
        Transaction transaction = new Transaction();
        BeanUtils.copyProperties(depositParam, transaction);
        transaction.setType(Transaction.TransactionType.DEPOSIT);
        transaction.setCreatedAt(new Date());
        log.debug("transaction="+ transaction);

        // 更新账户余额
        transactionMapper.deposit(transaction.getToAccountId(), transaction.getAmount());

        // 插入交易记录
        transactionMapper.insertTransaction(transaction);

        DepositVO depositVO = transactionMapper.selectNewDeposit(transaction);

        log.debug("返回结果:" + depositVO);
        return JsonResult.ok(depositVO);
    }

    @PostMapping("withdraw")
    @Operation(summary = "取款")
    @ApiOperationSupport(order = 20)
    public JsonResult withdraw(@RequestBody WithdrawParam withdrawParam) {
        log.debug("withdrawParam="+ withdrawParam);
        BigDecimal balance = transactionMapper.selectBalance(withdrawParam.getFromAccountId());
        if (balance.compareTo(withdrawParam.getAmount()) < 0) {
            return JsonResult.fail("余额不足");
        }
        Transaction transaction = new Transaction();
        BeanUtils.copyProperties(withdrawParam, transaction);
        transaction.setType(Transaction.TransactionType.WITHDRAW);
        transaction.setCreatedAt(new Date());


        // 更新账户余额
        transactionMapper.withdraw(transaction.getFromAccountId(), transaction.getAmount());

        // 插入交易记录
        transactionMapper.insertTransaction(transaction);

        WithdrawVO withdrawVO = transactionMapper.selectNewWithdraw(transaction);

        log.debug("返回结果:" + withdrawVO);
        return JsonResult.ok(withdrawVO);
    }

    @PostMapping("transfer")
    @Operation(summary = "转账")
    @ApiOperationSupport(order = 30)
    public JsonResult transfer(@RequestBody TransferParam transferParam) {
        log.debug("开始处理【转账】的业务，参数：" + transferParam);
        BigDecimal balance = transactionMapper.selectBalance(transferParam.getFromAccountId());
        if (balance.compareTo(transferParam.getAmount()) < 0) {
            return JsonResult.fail("余额不足,转账失败");
        }

        Transaction transaction = new Transaction();
        BeanUtils.copyProperties(transferParam, transaction);
        transaction.setType(Transaction.TransactionType.TRANSFER);
        transaction.setCreatedAt(new Date());

        transactionMapper.transferFrom(transferParam.getFromAccountId(),  transferParam.getAmount());
        transactionMapper.transferTo(transferParam.getToAccountId(), transferParam.getAmount());

        transactionMapper.insertTransaction(transaction);

        return JsonResult.ok();
    }

    @GetMapping("{transactionId}")
    @Operation(summary = "获取指定交易记录")
    @ApiOperationSupport(order = 40)
    public JsonResult getTransaction(@PathVariable("transactionId") Integer transactionId) {
        DetailTransactionVO detailTransactionVO = transactionMapper.selectDetail(transactionId);

        if (detailTransactionVO == null) {
            return JsonResult.fail("无该交易记录");
        }
        return JsonResult.ok(detailTransactionVO);
    }


}































